package ds_industry_2025.industry

import org.apache.spark.sql.SparkSession

//  todo 工业卷子的dwd层的表格准备 hive
object hive_dwd_table_perparation {
  def main(args: Array[String]): Unit = {
    val spark = SparkSession.builder()
      .master("local[*]")
      .appName("工业卷子的dwd层的表格准备")
      .config("hive.exec.dynamic.partition.mode","nonstrict")
      .config("spark.serializer","org.apache.spark.serializer.KryoSerializer")
      .config("spark.sql.extensions","org.apache.spark.sql.hudi.HoodieSparkSessionExtension")
      .enableHiveSupport()
      .getOrCreate()

    spark.sql("drop database if exists dwd cascade")
    spark.sql("create database if not exists dwd")
    spark.sql("use dwd")

    //  fact_environment_data
    spark.sql("drop table if exists fact_environment_data")
    spark.sql(
      """
        |create table if not exists fact_environment_data(
        |EnvoId string,
        |BaseID string,
        |CO2 string,
        |PM25 string,
        |PM10 string,
        |Temperature string,
        |Humidity string,
        |TVOC string,
        |CH2O string,
        |Smoke string,
        |InPutTime string,
        |dwd_insert_user string,
        |dwd_insert_time timestamp,
        |dwd_modify_user string,
        |dwd_modify_time timestamp
        |)
        |partitioned by(etldate string)
        |""".stripMargin)
    println("fact_environment_data表创建完成")

    //  fact_change_record
    spark.sql("drop table if exists fact_change_record")
    spark.sql(
      """
        |create table if not exists fact_change_record(
        |ChangeID int,
        |ChangeMachineID int,
        |ChangeMachineRecordID int,
        |ChangeRecordState string,
        |ChangeStartTime timestamp,
        |ChangeEndTime timestamp,
        |ChangeRecordData string,
        |ChangeHandleState int,
        |dwd_insert_user string,
        |dwd_insert_time timestamp,
        |dwd_modify_user string,
        |dwd_modify_time timestamp
        |)
        |partitioned by(etldate string)
        |""".stripMargin)
    println("fact_change_record表创建完成")

    //  dim_machine
    spark.sql("drop table if exists dim_machine")
    spark.sql(
      """
        |create table if not exists dim_machine(
        |BaseMachineID int,
        |MachineFactory int,
        |MachineNo string,
        |MachineName string,
        |MachineIP string,
        |MachinePort int,
        |MachineAddDate timestamp,
        |MachineRemarks string,
        |MachineAddEmpID int,
        |MachineResponsEmpID int,
        |MachineLedgerXml string,
        |ISWS int,
        |dwd_insert_user string,
        |dwd_insert_time timestamp,
        |dwd_modify_user string,
        |dwd_modify_time timestamp
        |)
        |partitioned by(etldate string)
        |""".stripMargin)
    println("dim_machine表创建完成")

    //  fact_produce_record
    spark.sql("drop table if  exists  fact_produce_record")
    spark.sql(
      """
        |create table if not exists fact_produce_record(
        |ProduceRecordID int,
        |ProduceMachineID int,
        |ProduceCodeNumber string,
        |ProduceStartWaitTime timestamp,
        |ProduceCodeStartTime timestamp,
        |ProduceCodeEndTime timestamp,
        |ProduceCodeCycleTime int,
        |ProduceEndTime timestamp,
        |ProduceTotalOut int,
        |ProduceInspect int,
        |dwd_insert_user string,
        |dwd_insert_time timestamp,
        |dwd_modify_user string,
        |dwd_modify_time timestamp
        |)
        |partitioned by(etldate string)
        |""".stripMargin)
    println("fact_produce_record表创建完成")

    //  fact_machine_data
    spark.sql("drop table if exists fact_machine_data")
    spark.sql(
      """
        |create table if not exists fact_machine_data(
        |MachineRecordID int,
        |MachineID int,
        |MachineRecordState string,
        |MachineRecordData string,
        |MachineRecordDate timestamp,
        |dwd_insert_user string,
        |dwd_insert_time timestamp,
        |dwd_modify_user string,
        |dwd_modify_time timestamp
        |)
        |partitioned by(etldate string)
        |""".stripMargin)
    println("fact_machine_data表创建完成")





    spark.close()
  }

}
